package com.hanhai.material.device.controller;

import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysWorkPlan;
import com.hanhai.material.device.entity.SysWorkShop;
import com.hanhai.material.device.service.SysWorkPlanService;
import com.hanhai.material.device.service.SysWorkShopService;
import com.hanhai.material.model.workshop.WorkShop;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;

import static com.hanhai.material.device.entity.table.SysWorkPlanTableDef.SYS_WORK_PLAN;
import static com.hanhai.material.device.entity.table.SysWorkShopTableDef.SYS_WORK_SHOP;
import static com.hanhai.material.utils.TimeUtils.getDatesOfMonth;
import static com.mybatisflex.core.query.QueryMethods.distinct;

/**
 *  控制层。
 *
 * @author youchuang
 * @since 2023/10/18 11:26:03
 */
@RestController
@RequestMapping("/work_shop")
public class SysWorkShopController {

    @Autowired
    private SysWorkShopService sysWorkShopService;

    @Autowired
    private SysWorkPlanService sysWorkPlanService;

   @PostMapping("/list/{workShopName}")
    public Result<?> list(@PathVariable String workShopName){
       YearMonth yearMonth=YearMonth.now();
       List<LocalDate> dateList=getDatesOfMonth(yearMonth);
       LocalDate start=dateList.get(0);
       LocalDate end=dateList.get(dateList.size()-1);
       QueryWrapper queryWrapper=QueryWrapper.create();
       List<WorkShop> workShopList=this.sysWorkShopService.listAs(queryWrapper, WorkShop.class);
       for(WorkShop workShop:workShopList){
           queryWrapper=QueryWrapper.create();
           queryWrapper.select(distinct(SYS_WORK_PLAN.WORK_SHOP).as("name"));
           queryWrapper.where(SYS_WORK_PLAN.WORK_SHOP.like(workShop.getName()));
           queryWrapper.where(SYS_WORK_PLAN.GENERATE_DATE_TIME.between(start,end));
           queryWrapper.orderBy(SYS_WORK_PLAN.WORK_SHOP,true);
           List<WorkShop> workShopList1=this.sysWorkPlanService.listAs(queryWrapper, WorkShop.class);
           for(WorkShop workShop1:workShopList1){
               queryWrapper=QueryWrapper.create();
               queryWrapper.select(distinct(SYS_WORK_PLAN.MACHINE_NAME).as("name"));
               queryWrapper.where(SYS_WORK_PLAN.WORK_SHOP.eq(workShop1.getName()));
               queryWrapper.where(SYS_WORK_PLAN.GENERATE_DATE_TIME.between(start,end));
               queryWrapper.orderBy(SYS_WORK_PLAN.MACHINE_NAME,true);
               List<WorkShop> workShopList2=this.sysWorkPlanService.listAs(queryWrapper, WorkShop.class);
//               for(WorkShop workShop2:workShopList2){
//                   queryWrapper=QueryWrapper.create();
//                   queryWrapper.select(distinct(SYS_WORK_PLAN.WORK_PLAN_ID).as("name"));
//                   queryWrapper.where(SYS_WORK_PLAN.MACHINE_NAME.eq(workShop2.getName()));
//                   List<WorkShop> workShopList3=this.sysWorkPlanService.listAs(queryWrapper, WorkShop.class);
//                   workShop2.setChildren(workShopList3);
//               }
               workShop1.setChildren(workShopList2);
           }
           workShop.setChildren(workShopList1);
       }
       return workShopList.isEmpty()? ResultTool.fail():ResultTool.success(workShopList);
   }

   @PostMapping("/get/{workShop}")
    public Result<?> get(@PathVariable String workShop){
       QueryWrapper queryWrapper=QueryWrapper.create();
       queryWrapper.where(SYS_WORK_SHOP.WORK_SHOP_NAME.like(workShop));
       queryWrapper.limit(1);
       SysWorkPlan sysWorkPlan=this.sysWorkPlanService.getOne(queryWrapper);
       return sysWorkPlan==null?ResultTool.fail():ResultTool.success(sysWorkPlan);
   }
   @PostMapping("/list")
    public Result<?> list(){
       List<SysWorkShop> sysWorkShopList=this.sysWorkShopService.list();
       return sysWorkShopList.isEmpty()?ResultTool.fail():ResultTool.success(sysWorkShopList);
   }
}
